home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / src / sjis.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  3.2 KB  |  114 lines

  1.  
  2. /*
  3.  * SHIFT-JIS
  4.  */
  5.  
  6. /*
  7.    Conversion between SJIS codes (s1,s2) and JISX0208 codes (c1,c2):
  8.    Example. (s1,s2) = 0x8140, (c1,c2) = 0x2121.
  9.    0x81 <= s1 <= 0x9F || 0xE0 <= s1 <= 0xEA,
  10.    0x40 <= s2 <= 0x7E || 0x80 <= s2 <= 0xFC,
  11.    0x21 <= c1 <= 0x74, 0x21 <= c2 <= 0x7E.
  12.    Invariant:
  13.      94*2*(s1 < 0xE0 ? s1-0x81 : s1-0xC1) + (s2 < 0x80 ? s2-0x40 : s2-0x41)
  14.      = 94*(c1-0x21)+(c2-0x21)
  15.    Conversion (s1,s2) -> (c1,c2):
  16.      t1 := (s1 < 0xE0 ? s1-0x81 : s1-0xC1)
  17.      t2 := (s2 < 0x80 ? s2-0x40 : s2-0x41)
  18.      c1 := 2*t1 + (t2 < 0x5E ? 0 : 1) + 0x21
  19.      c2 := (t2 < 0x5E ? t2 : t2-0x5E) + 0x21
  20.    Conversion (c1,c2) -> (s1,s2):
  21.      t1 := (c1 - 0x21) >> 1
  22.      t2 := ((c1 - 0x21) & 1) * 0x5E + (c2 - 0x21)
  23.      s1 := (t1 < 0x1F ? t1+0x81 : t1+0xC1)
  24.      s2 := (t2 < 0x3F ? t2+0x40 : t2+0x41)
  25.  */
  26.  
  27. static int
  28. sjis_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  29. {
  30.   unsigned char c = *s;
  31.   if (c < 0x80 || (c >= 0xa1 && c <= 0xdf))
  32.     return jisx0201_mbtowc(conv,pwc,s,n);
  33.   else {
  34.     unsigned char s1, s2;
  35.     s1 = c;
  36.     if ((s1 >= 0x81 && s1 <= 0x9f) || (s1 >= 0xe0 && s1 <= 0xea)) {
  37.       if (n < 2)
  38.         return RET_TOOFEW(0);
  39.       s2 = s[1];
  40.       if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) {
  41.         unsigned char t1 = (s1 < 0xe0 ? s1-0x81 : s1-0xc1);
  42.         unsigned char t2 = (s2 < 0x80 ? s2-0x40 : s2-0x41);
  43.         unsigned char buf[2];
  44.         buf[0] = 2*t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
  45.         buf[1] = (t2 < 0x5e ? t2 : t2-0x5e) + 0x21;
  46.         return jisx0208_mbtowc(conv,pwc,buf,2);
  47.       }
  48.     } else if (s1 >= 0xf0 && s1 <= 0xf9) {
  49.       /* User-defined range. See
  50.        * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  51.       if (n < 2)
  52.         return RET_TOOFEW(0);
  53.       s2 = s[1];
  54.       if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) {
  55.         *pwc = 0xe000 + 188*(s1 - 0xf0) + (s2 < 0x80 ? s2-0x40 : s2-0x41);
  56.         return 2;
  57.       }
  58.     }
  59.     return RET_ILSEQ;
  60.   }
  61. }
  62.  
  63. static int
  64. sjis_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  65. {
  66.   unsigned char buf[2];
  67.   int ret;
  68.  
  69.   /* Try JIS X 0201-1976. */
  70.   ret = jisx0201_wctomb(conv,buf,wc,1);
  71.   if (ret != RET_ILSEQ) {
  72.     unsigned char c;
  73.     if (ret != 1) abort();
  74.     c = buf[0];
  75.     if (c < 0x80 || (c >= 0xa1 && c <= 0xdf)) {
  76.       r[0] = c;
  77.       return 1;
  78.     }
  79.   }
  80.  
  81.   /* Try JIS X 0208-1990. */
  82.   ret = jisx0208_wctomb(conv,buf,wc,2);
  83.   if (ret != RET_ILSEQ) {
  84.     unsigned char c1, c2;
  85.     if (ret != 2) abort();
  86.     if (n < 2)
  87.       return RET_TOOSMALL;
  88.     c1 = buf[0];
  89.     c2 = buf[1];
  90.     if ((c1 >= 0x21 && c1 <= 0x74) && (c2 >= 0x21 && c2 <= 0x7e)) {
  91.       unsigned char t1 = (c1 - 0x21) >> 1;
  92.       unsigned char t2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
  93.       r[0] = (t1 < 0x1f ? t1+0x81 : t1+0xc1);
  94.       r[1] = (t2 < 0x3f ? t2+0x40 : t2+0x41);
  95.       return 2;
  96.     }
  97.   }
  98.  
  99.   /* User-defined range. See
  100.    * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  101.   if (wc >= 0xe000 && wc < 0xe758) {
  102.     unsigned char c1, c2;
  103.     if (n < 2)
  104.       return RET_TOOSMALL;
  105.     c1 = (unsigned int) (wc - 0xe000) / 188;
  106.     c2 = (unsigned int) (wc - 0xe000) % 188;
  107.     r[0] = c1+0xf0;
  108.     r[1] = (c2 < 0x3f ? c2+0x40 : c2+0x41);
  109.     return 2;
  110.   }
  111.  
  112.   return RET_ILSEQ;
  113. }
  114.